package com.jy.modules.cims.quota.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.JResult;
import com.jy.modules.cims.common.controller.TCMmQuotaController;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmQuotaService;
import com.jy.modules.cims.contract.service.ContractService;
import com.jy.modules.cims.data.common.dto.TCMmQuotaCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmQuotaDTO;
import com.jy.modules.cims.data.dto.QuotaCreditDetailDto;
import com.jy.modules.cims.data.dto.QuotaCreditRightDto;
import com.jy.modules.cims.data.dto.QuotaDto;
import com.jy.modules.cims.data.dto.QuotaQueryDto;
import com.jy.modules.cims.deal.service.DealService;
import com.jy.modules.cims.quota.service.QuotaService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.web.base.BaseController;

/**
 * 额度处理控制层
 */
@Controller
@Scope("prototype")
@RequestMapping("/quota")
public class QuotaController extends BaseController {

	/**
	 * 额度处理控制层
	 */
	public QuotaController() {
	}

	/**
	 * 交易服务
	 */
	public DealService dealService;

	/**
	 * 合约处理服务
	 */
	public ContractService contractService;

	/**
	 * 额度处理服务
	 */
	@Autowired
	@Qualifier("com.jy.modules.cims.quota.service.QuotaService")
	private QuotaService quotaService;

	@Autowired
	private TCMmQuotaController quotaController;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmQuotaService")
	private TCMmQuotaService tCMmQuotaService;

	/**
	 * 额度查询列表
	 * 
	 * @param request
	 * @param dto
	 *            查询条件实体
	 * @param dataMsg
	 * @return
	 */
	@RequestMapping(value = "/getQuotaList")
	@ResponseBody
	public DataMsg getQuotaList(HttpServletRequest request, QuotaQueryDto dto, @ModelAttribute DataMsg dataMsg)
			throws Exception {
		quotaController.queryListTCMmQuota(request, dto, dataMsg);
		// dataMsg.setData(quotaService.getQuotaList(dto));
		return dataMsg;
	}

	/**
	 * @author JY-IT-D001
	 * @description:查询分页列表
	 * @date 2015-01-09 09:48:13
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryListQuotaCreditDetail")
	@ResponseBody
	public DataMsg queryListTCMmQuotaCreditDetail(HttpServletRequest request, TCMmQuotaCreditDetailDTO dto,
			@ModelAttribute DataMsg dataMsg) throws Exception {

		Map<String, Object> searchParams = new HashMap<String, Object>();
		QuotaCreditRightDto quotaCreditRightDto = new QuotaCreditRightDto();
		quotaCreditRightDto.setQuotaCode(dto.getQuotaCode());
		quotaCreditRightDto.settCMmQuotaDTO(new TCMmQuotaDTO());
		searchParams.put("dto", quotaCreditRightDto);
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(searchParams);
		PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);

		List<QuotaCreditDetailDto> list = quotaService.searchQuotaCreditDetailByPaging(params.getSearchParams());

		dataMsg.setData(list);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
		return dataMsg;
	}

	/**
	 * 额度明细查询列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public DataMsg getQuotaDetailList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO implement here
		return null;
	}

	/**
	 * 额度申请保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/quotaApply")
	@ResponseBody
	public DataMsg quotaApply(HttpServletRequest request, QuotaDto dto, @ModelAttribute DataMsg dataMsg)
			throws Exception {
		dataMsg = super.initDataMsg(dataMsg);
		/*
		 * dto.setQuotaType(CimsConstant.QUOTATYPE_LOAN);
		 * dto.setContractCode("1"); dto.setCooperorgCode("1");
		 * dto.setQuotaSum(new BigDecimal("1000.90"));
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		String businessCode = null;
		String errorMessage = null;
		String doStartWorkFlag = "1";
		try {
			JResult result = quotaService.quotaApply(dto);
			businessCode = dto.getQuotaCode();
			if (!result.isSuccess()) {
				doStartWorkFlag = "0";
				errorMessage = result.getErrMsg();
				dataMsg.failed("");
			}
		} catch (Exception e) {
			String message = e.getMessage();
			businessCode = message.split(";")[0];
			errorMessage = message.split(";")[1];
			dataMsg.failed("");
		}
		map.put("doStartWorkFlag", doStartWorkFlag);
		map.put("businessCode", businessCode);
		map.put("errorMessage", errorMessage);
		dataMsg.setData(map);
		return dataMsg;
	}

	/**
	 * 额度申请释放
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public DataMsg quotaUnlock(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO implement here
		return null;
	}

	/**
	 * 删除额度申请
	 * 
	 * @param request
	 * @param response
	 */
	public void deleteQuota(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO implement here
	}

	/**
	 * 分配额度明细给指定交易
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public DataMsg allocToDeal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO implement here
		return null;
	}
	
	/**
	 *  额度申请-审核
	 * @param request
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/reviewedApply")
	@ResponseBody
	public DataMsg reviewedApply(HttpServletRequest request, @ModelAttribute DataMsg dataMsg)
			throws Exception {
		dataMsg = super.initDataMsg(dataMsg);
		
		String quotaCode = this.getParameterString("quotaCode");// 申请流水号
		String creditReferDate = this.getParameterString("creditReferDate");// 债权标的日期
		Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("quotaCode", quotaCode);
        
		try {
			TCMmQuotaDTO quotaDto = new TCMmQuotaDTO();
			quotaDto.setQuotaCode(quotaCode);
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams.put("dto", quotaDto);
			List<TCMmQuotaDTO> dataList = tCMmQuotaService.searchTCMmQuota(searchParams);
			if (CollectionsUtils.isNotEmpty(dataList) && SimpleStringUtils.hasText(creditReferDate)) {
				quotaDto = dataList.get(0);
				if (CimsConstant.QUOTATYPE_CR.equals(quotaDto.getQuotaType())) {
					quotaDto.setCreditReferDate(DateTimeUtils.parse(creditReferDate, DateTimeUtils.DEFAULT_DATE_FORMAT_2));
					tCMmQuotaService.updateTCMmQuota(quotaDto);
				}
			}
			 //额度申请-审核 调合同调配以及更新数据库入口
			 quotaService.updateQuotaApply(quotaCode);
			 dataMsg.setMsg("审核成功！");
			 
		} catch (Exception e) {
			
			 dataMsg.setMsg("审核失败！"+e.getMessage());
		}
		 dataMsg.setData(resultMap);
		 
		return dataMsg;
	}

}